package com.gaogzhen.cas;

import java.util.ArrayList;
import java.util.List;

/**
 * 账户接口
 */
public interface Account {
    // 获取余额
    Integer getBalance();
    // 取款
    void withdraw(Integer amount);
    // 模拟多个线程取款操作
    static void multiWithdraw(Account account) {
        List<Thread> ts = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            ts.add(new Thread(() -> {
                account.withdraw(10);
            }));
        }
        long start = System.currentTimeMillis();
        ts.forEach(Thread::start);
        ts.forEach(t -> {
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        long end = System.currentTimeMillis();
        System.out.println("balance: "+ account.getBalance() + " cost: " + (end - start) + " ms");
    }
}
